package com.jiangxiaofei.citycloud.activity.fleamarket;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.EmptyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.GoodDetailBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FleaConstant;
import com.jiangxiaofei.citycloud.mvp.FleaPersenter;
import com.jiangxiaofei.citycloud.param.FleaGetDetailParam;
import com.jiangxiaofei.citycloud.util.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXfei
 * @date: 2017/10/31 0031
 * @title: 二手物资商品详情页
 */

public class FleaMarketGoodsDetailActivity extends BaseActivity
        implements FleaConstant.GetGoodDetailView {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tv_price)
    TextView tvPrice;
    @InjectView(R.id.tv_visitors)
    TextView tvVisitors;
    @InjectView(R.id.tv_distance)
    TextView tvDistance;
    @InjectView(R.id.tv_descrption)
    TextView tvDescrption;
    @InjectView(R.id.recy_photo)
    RecyclerView recyPhoto;
    @InjectView(R.id.tv_call)
    TextView tvCall;
    @InjectView(R.id.tv_qq)
    TextView tvQq;

    private FleaConstant.GetGoodDetailPersenter getGoodDetailPersenter;
    private int id = 0;
    private FleaGetDetailParam param;
    private List<String> imgList = new ArrayList<>();
    private ImgLoadeRecyAdapter imgLoadeRecyAdapter;
    private String phoneNum;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_flea_marker_goods_detail);
        mContext = this;
        mActivity = this;
        ButterKnife.inject(this);
        initTitle();

        getGoodDetailPersenter = new FleaPersenter(mContext, (FleaConstant.GetGoodDetailView) mActivity);
        Intent intent = getIntent();
        id = intent.getIntExtra(StringConstants.FLEAGOODID, 0);
        if (id != 0) {
            BDLocation bdLocation;
            if (EmptyUtils.isNotEmpty(Constants.bdLocation)) {
                bdLocation = Constants.bdLocation;
            } else {
                Constants.bdclient.start();
                bdLocation = Constants.bdLocation;
            }
            param = new FleaGetDetailParam(id, String.valueOf(bdLocation.getLongitude()), String.valueOf(bdLocation.getLatitude()));
            getGoodDetailPersenter.getGoodDetail();
        }
        initImgAdapter();
    }

    private void initImgAdapter() {
        imgLoadeRecyAdapter = new ImgLoadeRecyAdapter(R.layout.item_flea_good_image, imgList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyPhoto.setLayoutManager(linearLayoutManager);
        recyPhoto.setAdapter(imgLoadeRecyAdapter);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("商品信息");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public FleaGetDetailParam getGoodDetailParam() {
        return param;
    }

    @Override
    public void getGoodDetailOnSuccess(List<GoodDetailBean> goodDetailBeans) {
        if (goodDetailBeans.size() > 0) {
            GoodDetailBean fleaGoodDetailBean = goodDetailBeans.get(0);
            tvTitle.setText(fleaGoodDetailBean.getTitle());
            tvDescrption.setText(fleaGoodDetailBean.getDescription());
            tvDistance.setText("距离" + fleaGoodDetailBean.getDistance());
            tvPrice.setText(fleaGoodDetailBean.getPrice());
            tvVisitors.setText(fleaGoodDetailBean.getVisitors());
            String[] split = fleaGoodDetailBean.getImg().split(",");
            imgList.clear();
            for (int i = 0; i < split.length; i++) {
                imgList.add(split[i]);
            }
            imgLoadeRecyAdapter.notifyDataSetChanged();
            phoneNum = fleaGoodDetailBean.getPhonenum();
        }
    }

    private class ImgLoadeRecyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public ImgLoadeRecyAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            ImageView imgView = helper.getView(R.id.img_view);
            Glide.with(mContext).load(item).into(imgView);
        }
    }

    @OnClick({R.id.tv_call, R.id.tv_qq})
    public void bOnclick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_call:
                //联系卖家电话
//                PermissionUtil.CallPhone(mContext, phoneNum);

//                PermissionUtil.sendMsg(mContext,phoneNum);
                initDetailPopup(v1 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.CallPhone(mContext, phoneNum);
                }, v2 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.sendMsg(mContext, phoneNum);
                }, v3 -> {
                    mPopupWindowPublish.dismiss();
                });
                showDetailPopup();
                break;
            case R.id.tv_qq:
                //QQ联系
                String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + "303250762";
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                break;
        }
    }

}
